package main

import (
	"encoding/xml"
	"fmt"
	"strings"
)

// 使用xml包解析xml字符串

func main() {
	input := "<Person><GivenName>Meimei</GivenName><Surname>Han</Surname></Person>"
	inputReader := strings.NewReader(input)
	dec := xml.NewDecoder(inputReader)
	for t, err := dec.Token(); err == nil; t, err = dec.Token() {
		switch token := t.(type) {
		case xml.StartElement:
			name := token.Name.Local
			fmt.Printf("Token Name is %s\n", name)
			// 打印标签属性
			for _, attr := range token.Attr {
				attrName := attr.Name.Local
				attrValue := attr.Value
				fmt.Printf("The attribute %s: %s\n", attrName, attrValue)
			}
		case xml.EndElement:
			fmt.Println("The end of token")
		case xml.CharData:
			content := string([]byte(token))
			fmt.Printf("The Content: %v\n", content)
		}
	}
}
